//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    canIUse:true,
    contactList: [
    
    ],

  },

  itemClick: function (item) {
    let itemInfo = item.currentTarget.dataset.item;
    if (!itemInfo.Shield){
      var that = this
      app.globalData.itemInfo = itemInfo,
      wx.redirectTo({
        url: '../personal/personal',
      })

    }
    
  },


  setShield: function(e) {
    let item = e.currentTarget.dataset.item;
    let index = this.getIndex(item.Id);
    console.log(index)

    if (this.data.contactList[index].Shield) {
      this.requestCardShield(index,item.CardId,false);
    } else {
      this.requestCardShield(index,item.CardId, true);
    }
  },

  setTop: function (e) {
    let item = e.currentTarget.dataset.item;
    let index = this.getIndex(item.Id);

    let parm = {};
    if (this.data.contactList[index].TopFlg) {
      this.requestTop(index,item.CardId,false);      
    } else {
      this.requestTop(index, item.CardId, true);
    }

    this.setData(parm)
  },

  

  getIndex: function(id){
    let _data = this.data.contactList;
    console.log(_data);
    for (let i = 0; i < _data.length; i++) {
      if (_data[i].Id == id) {
        return i;
      }
    }
  },

  getTopNum: function (_data) {
    console.log(_data);
    let a = 0;
    for (let i = 0; i < _data.length; i++) {
      if (_data[i].TopFlg) {
         a++;
      }
    }
    return a;
  },

  showView: function (display) {
    this.setData({
      display: "block"
    })
  },

  hideView: function (display) {
    this.setData({
      display: "none"
    })
  },


  requestCardShield: function (index,cardId, Shield){
    var that = this;
    wx.request({
      url: app.globalData.url + "CardShield", 
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      data: {
        "CardId": cardId,
        "Shield": Shield
      },
      method: 'GET',
      success: function (res) {
        console.log("requestCardShield====" + res.data.ResultObject)

        if(res.data.Code == 1){
          let parm = {};
          parm['contactList[' + index + '].Shield'] = Shield;
          that.setData(parm);
        } else {
          wx.showToast({
            title: res.data.Msg,
          })
        }

      }
    })

  },


  requestTop: function (index, cardId, Top) {
    var that = this;
    wx.request({
      url: app.globalData.url + "CardTop", 
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      data: {
        "CardId": cardId,
        "Top" : Top,
      },
      method: 'GET',
      success: function (res) {
        let _data = that.data.contactList;
        console.log(res);
        if (res.data.Code == 1) {
          let _item = _data[index];
          _item.TopFlg = Top;
          _data.splice(index, 1)
          if (Top){
            _data.unshift(_item);
          } else {
            _data.splice(that.getTopNum(_data) ,0, _item)
          }

          that.setData({
            contactList: _data,
          })

        } else {
          wx.showToast({
            title: res.data.Msg,
          })
        }

      }
    })

  },

  getCardList: function () {
    console.log("getCardList");
    var that = this;
    wx.request({
      url: app.globalData.url + "GetCardList", //仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      data: {
        "UserId": app.globalData.userInfo.Id,
        "Tocken": app.globalData.userInfo.Tocken
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data)
        that.setData({
          contactList: res.data.ResultObject,
        })

      }
    })
  },


  onLoad: function () {

    //登录后回来直接请求数据
    if (app.globalData.isLogin){
      this.getCardList();
    }

  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  login: function () {
    this.getCardList();
  }
})


